/*
 By:Hurry
 2009-12-28
 hurrry@gmail.com
*/


/*
//Sample Code:
//Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<%@ Register Assembly="ServerControls" Namespace="TechExcel.KnowledgeWise.ServerControls" TagPrefix="TechExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test Page</title>
<script type="text/javascript">
function onCheckNode(obj)
{
    //alert(obj.id.replace("ckb_",""));
    gid("msg").innerHTML="You click:"+obj.id.replace("ckb_","");
    return true;
}
function GetCheckedNodes() 
{
    var list = gid("tree").getElementsByTagName("input");
    var result = new Array();
    for(var i=0;i<list.length;i++)
    {
        if (list[i].checked)
        {
        result.push(list[i].id.replace("ckb_",""));
        }
    }
    alert(result.join(","));
}
function onHeadTdClick(td)
{
    alert(td.innerHTML);
}
function onMouseOut(obj)
{
    obj.style.backgroundColor="";
}
function onMouseOver(obj)
{
    obj.style.backgroundColor="#cccccc";
}
function CheckAll(obj)
{
    var list = gid("tree").getElementsByTagName("input");
    var result = new Array();
    for(var i=0;i<list.length;i++)
    {
        list[i].checked=obj.checked;
        
    }
}
</script>

</head>
<body style="padding:10px;">
<form id="form1" runat="server">
<div >
<TechExcel:TreeListView ID="tree" runat="server" />
    <br />
    <br />
    <asp:CheckBox ID="ckbShowTreeLine" runat="server" AutoPostBack="True" Checked="true" Text="Show Tree Line" />
    <asp:CheckBox ID="ckbShowCheckBox" runat="server" AutoPostBack="True" Checked="true" Text="Show Check Box" /><br />
    <br />
<asp:Button ID="Button1" runat="server" Text="GetCheckedNodes" OnClientClick="GetCheckedNodes();return false;" />
<br /><br />
<span id='msg' style="color:Red;"></span>
</div>
</form>
</body>
</html>




///////////////////////////////////////////////////////////////////////////////////////
//Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TechExcel.KnowledgeWise.ServerControls;

public partial class Test : PageBase
{
    private bool _showTreeLine=true;
    private bool _showCheckBox=true;

    protected void Page_Load(object sender, EventArgs e)
    {
        _showCheckBox = this.ckbShowCheckBox.Checked;
        _showTreeLine = this.ckbShowTreeLine.Checked;

        TreeListNode head = new TreeListNode();
        head.RowInfo=new TreeListColumn("a");
        head.ShowCheckBox = _showCheckBox;
        head.OnCheckedFunName = "CheckAll";
        TreeListColumn td1 = new TreeListColumn("head column 1");
        td1.CssClass = "table_3rdheader";
        td1.Align = "left";
        TreeListColumn td2 = new TreeListColumn("head column 2");
        td2.CssClass = "table_3rdheader";
        td2.Align = "center";
        td2.Width = "200px";
        TreeListColumn td3 = new TreeListColumn("head column 3");
        td3.CssClass = "table_3rdheader";
        td3.Align = "center";
        td3.OnClientClick = "onHeadTdClick";
        td3.Width = "200px";


        head.Columns.Add(td1);
        head.Columns.Add(td2);
        head.Columns.Add(td3);
        

        TreeListNode row1 = new TreeListNode();
        row1.ShowCheckBox = _showCheckBox;
        row1.IconUrl = "images/tree_Specfication_close.gif";
        row1.RowInfo.ID = "1";
        row1.RowInfo.CssClass = "table_oddrow";
        row1.OnCheckedFunName = "onCheckNode";
        row1.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row1.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");
        row1.RowInfo.Attributes.Add("style","text-decoration: line-through;");

        TreeListColumn td11 = new TreeListColumn("1 column 1");
        td11.CssClass = "table_oddrow_td";
        td11.Height = "25px";
        TreeListColumn td12 = new TreeListColumn("1 column 2");
        td12.CssClass = "table_oddrow_td";
        TreeListColumn td13 = new TreeListColumn("1 column 3");
        td13.CssClass = "table_oddrow_td";

        row1.Columns.Add(td11);
        row1.Columns.Add(td12);
        row1.Columns.Add(td13);

        TreeListNode row2 = new TreeListNode();
        row2.RowInfo.ID = "12";
        row2.ShowCheckBox = _showCheckBox;
        row2.IconUrl = "images/tree_Specfication_close.gif";
        row2.RowInfo.CssClass = "table_evenrow";
        row2.OnCheckedFunName = "onCheckNode";
        row2.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row2.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");

        TreeListColumn td21 = new TreeListColumn("2 column 1");
        td21.CssClass = "table_evenrow_td";
        td21.Height = "30px";
        TreeListColumn td22 = new TreeListColumn("2 column 2");
        td22.CssClass = "table_evenrow_td";
        TreeListColumn td23 = new TreeListColumn("2 column 3");
        td23.CssClass = "table_evenrow_td";

        row2.Columns.Add(td21);
        row2.Columns.Add(td22);
        row2.Columns.Add(td23);


        TreeListNode row3 = new TreeListNode();
        row3.RowInfo.ID = "13";
        row3.ShowCheckBox = _showCheckBox;
        row3.IconUrl = "images/tree_Specfication_close.gif";
        row3.RowInfo.CssClass = "table_evenrow";
        row3.OnCheckedFunName = "onCheckNode";
        row3.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row3.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");

        TreeListColumn td31 = new TreeListColumn("3 column 1");
        td31.CssClass = "table_evenrow_td";
        td31.Height = "40px";
        TreeListColumn td32 = new TreeListColumn("3 column 2");
        td32.CssClass = "table_evenrow_td";
        TreeListColumn td33 = new TreeListColumn("3 column 3");
        td33.CssClass = "table_evenrow_td";
        row3.Columns.Add(td31);
        row3.Columns.Add(td32);
        row3.Columns.Add(td33);

        TreeListNode row22 = new TreeListNode();
        row22.RowInfo.ID = "122";
        row22.ShowCheckBox = _showCheckBox;
        row22.IconUrl = "images/tree_Specfication_close.gif";
        row22.RowInfo.CssClass = "table_oddrow";
        row22.OnCheckedFunName = "onCheckNode";
        row22.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row22.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");

        TreeListColumn td221 = new TreeListColumn("22 column 1");
        td221.CssClass = "table_oddrow_td";
        td221.Height = "50px";
        TreeListColumn td222 = new TreeListColumn("22 column 2");
        td222.CssClass = "table_oddrow_td";
        TreeListColumn td223 = new TreeListColumn("22 column 3");
        td223.CssClass = "table_oddrow_td";
        row22.Columns.Add(td221);
        row22.Columns.Add(td222);
        row22.Columns.Add(td223);


        TreeListNode row24 = new TreeListNode();
        row24.RowInfo.ID = "24";
        row24.ShowCheckBox = _showCheckBox;
        row24.IconUrl = "images/tree_Specfication_close.gif";
        row24.RowInfo.CssClass = "table_evenrow";
        row24.OnCheckedFunName = "onCheckNode";
        row24.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row24.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");

        TreeListColumn td241 = new TreeListColumn("24 column 1");
        td241.CssClass = "table_evenrow_td";
        td241.Height = "50px";
        TreeListColumn td242 = new TreeListColumn("24 column 2");
        td242.CssClass = "table_evenrow_td";
        TreeListColumn td243 = new TreeListColumn("24 column 3");
        td243.CssClass = "table_evenrow_td";

        row24.Columns.Add(td241);
        row24.Columns.Add(td242);
        row24.Columns.Add(td243);


        TreeListNode row23 = new TreeListNode();
        row23.RowInfo.ID = "23";
        row23.ShowCheckBox = _showCheckBox;
        row23.IconUrl = "images/tree_Specfication_close.gif";
        row23.RowInfo.CssClass = "table_oddrow";
        row23.OnCheckedFunName = "onCheckNode";
        row23.RowInfo.Attributes.Add("onmouseover", "onMouseOver(this);");
        row23.RowInfo.Attributes.Add("onmouseout", "onMouseOut(this);");

        TreeListColumn td231 = new TreeListColumn("23 column 1");
        td231.CssClass = "table_oddrow_td";
        td231.Height = "60px";
        TreeListColumn td232 = new TreeListColumn("23 column 2");
        td232.CssClass = "table_oddrow_td";
        TreeListColumn td233 = new TreeListColumn("23 column 3");
        td233.CssClass = "table_oddrow_td";
        row23.Columns.Add(td231);
        row23.Columns.Add(td232);
        row23.Columns.Add(td233);


        
        row1.ChildNodes.Add(row2);
        row1.ChildNodes.Add(row3);
        row2.ChildNodes.Add(row22);
        row2.ChildNodes.Add(row24);
        row2.ChildNodes.Add(row23);

        this.tree.Header = head;
        this.tree.Nodes.Add(row1);
        this.tree.CssClass = "table_border_bottom";
        this.tree.Border = "0";
        this.tree.CellPadding = "0";
        this.tree.CellSpacing = "0";
        this.tree.Width = "100%";
        this.tree.ShowTreeLine = _showTreeLine;
    }
}



*/


namespace Home.ServerControls
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;

    public class TreeListView : WebControl
    {
        private int _nodeIndex = 0;


        private List<TreeListNode> _nodes = new List<TreeListNode>();
        public List<TreeListNode> Nodes
        {
            get { return _nodes; }
            set { _nodes = value; }
        }

        private TreeListNode _header = null;
        public TreeListNode Header
        {
            get { return _header; }
            set { _header = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.WriteBeginTag("table");
            writer.WriteAttribute("id", this.ID);
            if (!string.IsNullOrEmpty(this.CssClass))
            {
                writer.WriteAttribute("class", this.CssClass);
            }
            if (!string.IsNullOrEmpty(this.Border))
            {
                writer.WriteAttribute("border", this.Border);
            }
            if (!string.IsNullOrEmpty(this.CellPadding))
            {
                writer.WriteAttribute("cellpadding", this.CellPadding);
            }
            if (!string.IsNullOrEmpty(this.CellSpacing))
            {
                writer.WriteAttribute("cellspacing", this.CellSpacing);
            }
            if (!string.IsNullOrEmpty(this.Width))
            {
                writer.WriteAttribute("width", this.Width);
            }

            if (!string.IsNullOrEmpty(this.Height))
            {
                writer.WriteAttribute("height", this.Height);
            }

            foreach (KeyValuePair<string, string> keyvalue in this.Attributes)
            {
                writer.WriteAttribute(keyvalue.Key, keyvalue.Value);
            }

            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Write("\n");
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            StringBuilder sb = new StringBuilder();
            if (this.Header != null)
            {
                this.Header.Depth = 0;
                BuildNode(sb, this.Header);
            }
            else
            {
                _nodeIndex++;
            }

            if (this.Nodes.Count > 0)
            {
                this.Nodes[this.Nodes.Count - 1].IsLastNode = true;
            }
            foreach (TreeListNode node in this.Nodes)
            {
                node.RowInfo.ExpandIconUrl = this.ExpandIconUrl;
                node.RowInfo.CollapseIconUrl = this.CollapseIconUrl;
                node.Depth = 1;
                BuildNode(sb, node);
            }

            writer.Write(sb.ToString());
        }

        public void BuildNode(StringBuilder sb, TreeListNode node)
        {
            if (node == null)
            {
                return;
            }

            sb.Append("<tr");
            BuildColumn(sb, node.RowInfo, 1, node.Depth);
            if (node.Depth > 0)
            {
                sb.AppendFormat(" childids='{0}'", node.GetChilds());
                sb.AppendFormat(" depth='{0}'", node.Depth);
                sb.AppendFormat(" parentid='{0}'", GetParentID(node));
            }
            sb.Append(" >");
            int nIndex = 0;
            foreach (TreeListColumn column in node.Columns)
            {
                sb.Append("<td");
                BuildColumn(sb, column, nIndex, node.Depth);
                sb.Append(" >");

                #region No Tree Line
                if (!this.ShowTreeLine)
                {
                    if (node.Depth > 0 && nIndex == 0)
                    {
                        sb.AppendFormat("<span style='padding-left:{0}px;'></span>", (node.Depth - 1) * 16);
                        if (node.ChildNodes.Count > 0)
                        {
                            //sb.AppendFormat("<img border='0' src='{0}' />", node.RowInfo.CollapseIconUrl);
                            sb.AppendFormat("<img src=\"{0}\" id='img_{1}' onClick=\"TreeListViewNodeClick(this);\" class='tlv_expandimg'>", node.RowInfo.CollapseIconUrl, node.RowInfo.ID);
                        }
                        else
                        {
                            sb.AppendFormat("<span style='padding-left:{0}px;'></span>", 9);
                        }
                    }
                    if (nIndex == 0 && node.ShowCheckBox)
                    {
                        sb.AppendFormat("<input type='checkbox' id='ckb_{0}'", node.RowInfo.ID);
                        if (node.IsChecked)
                        {
                            sb.Append(" checked ");
                        }
                        if (!string.IsNullOrEmpty(node.OnCheckedFunName))
                        {
                            sb.AppendFormat(" onclick='return {0}(this);'", node.OnCheckedFunName);
                        }
                        sb.Append("/>");
                    }
                    if (node.Depth > 0 && nIndex == 0)
                    {
                        if (!string.IsNullOrEmpty(node.IconUrl))
                        {
                            sb.AppendFormat("<img border='0' src='{0}' />", node.IconUrl);
                        }
                        foreach (TreeListIcon icon in node.Icons)
                        {
                            sb.AppendFormat("<img border='0' src='{0}' />", icon.URL);
                        }
                    }
                    sb.Append(column.Text);
                }
                #endregion

                #region Show Tree Line
                else
                {
                    if (node.Depth > 0 && nIndex == 0)
                    {
                        sb.Append("<table class='tlv_treelinetable'><tr><td height='100%'><table class='tlv_treelinetable'><tr>");
                        for (int i = 1; i < node.Depth; i++)
                        {
                            sb.AppendFormat("<td rowspan='3' class='{0}'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>", GetParentLine(i, node));
                        }

                        sb.AppendFormat("<td class='{0}'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>", GetCenterTopLine(node));
                        //sb.Append("<td rowspan='3' class='rightline'></td>");
                        sb.Append("</tr><tr>");

                        GetCenterHtml(sb, node);

                        sb.Append("</tr>");
                        sb.Append("<tr>");

                        //sb.Append("<td></td>");
                        GetBottomHtml(sb, node);


                        sb.Append("</tr>");
                        sb.Append("</table></td>");
                    }
                    if (nIndex == 0 && node.ShowCheckBox)
                    {
                        if (_nodeIndex > 0)
                        {
                            sb.Append("<td>");
                        }
                        sb.AppendFormat("<input type='checkbox' id='ckb_{0}'", node.RowInfo.ID);
                        if (node.IsChecked)
                        {
                            sb.Append(" checked ");
                        }
                        if (!string.IsNullOrEmpty(node.OnCheckedFunName))
                        {
                            sb.AppendFormat(" onclick='return {0}(this);'", node.OnCheckedFunName);
                        }
                        sb.Append("/>");
                        if (_nodeIndex > 0)
                        {
                            sb.Append("</td>");
                        }
                    }
                    if (node.Depth > 0 && nIndex == 0)
                    {
                        if (!string.IsNullOrEmpty(node.IconUrl))
                        {
                            sb.Append("<td>");
                            sb.AppendFormat("<img border='0' id='i_{1}' src='{0}' />", node.IconUrl, node.RowInfo.ID);
                            sb.Append("</td>");
                        }
                        int nIconIndex = 0;
                        foreach (TreeListIcon icon in node.Icons)
                        {
                            sb.Append("<td>");
                            sb.AppendFormat("<img border='0' id='i_{1}_{2}' src='{0}' />", icon.URL, node.RowInfo.ID, nIconIndex);
                            sb.Append("</td>");
                            nIconIndex++;
                        }
                        sb.AppendFormat("<td id='t_{0}'>", node.RowInfo.ID);
                        sb.Append(column.Text);
                        sb.Append(" </td></tr></table>");
                    }
                    else
                    {
                        sb.Append(column.Text);
                    }
                }
                #endregion

                sb.Append("</td>");
                nIndex++;
            }
            sb.Append("</tr>\n");

            _nodeIndex++;
            if (node.ChildNodes.Count > 0)
            {
                ((TreeListNode)node.ChildNodes[node.ChildNodes.Count - 1]).IsLastNode = true;
            }
            foreach (TreeListNode item in node.ChildNodes)
            {
                item.RowInfo.CollapseIconUrl = node.RowInfo.CollapseIconUrl;
                item.RowInfo.ExpandIconUrl = node.RowInfo.ExpandIconUrl;
                item.Depth = node.Depth + 1;
                BuildNode(sb, item);
            }

        }

        private void BuildColumn(StringBuilder sb, TreeListColumn column, int nIndex, int depth)
        {
            if (column == null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(column.ID))
            {
                sb.AppendFormat(" id='{0}'", column.ID);
            }
            if (!string.IsNullOrEmpty(column.Height))
            {
                sb.AppendFormat(" height='{0}'", column.Height);
            }
            else
            {
                if (depth > 0 && nIndex == 0)
                {
                    sb.Append(" height='100%'");
                }
            }
            if (!string.IsNullOrEmpty(column.Width))
            {
                sb.AppendFormat(" width='{0}'", column.Width);
            }
            if (!string.IsNullOrEmpty(column.CssClass))
            {
                sb.AppendFormat(" class='{0}'", column.CssClass);
            }
            if (!string.IsNullOrEmpty(column.Align))
            {
                sb.AppendFormat(" align='{0}'", column.Align);
            }

            if (!string.IsNullOrEmpty(column.OnClientClick))
            {
                sb.AppendFormat(" onclick=\"return {0}(this);\"", column.OnClientClick);
            }

            foreach (KeyValuePair<string, string> keyvalue in column.Attributes)
            {
                sb.AppendFormat(" {0}='{1}'", keyvalue.Key, keyvalue.Value);
            }
        }

        private string GetParentID(TreeListNode node)
        {
            if (node.Parent == null)
            {
                return "";
            }
            else
            {
                return ((TreeListNode)node.Parent).RowInfo.ID;

            }
        }

        private string GetParentLine(int depth, TreeListNode node)
        {
            TreeNode parent = node;
            for (int i = node.Depth; i > depth; i--)
            {
                parent = parent.Parent;
            }

            if (((TreeListNode)parent).IsLastNode)
            {
                return "tlv_verticalempty";
            }
            else
            {
                return "tlv_verticalline";
            }
        }

        private string GetCenterTopLine(TreeListNode node)
        {
            if (_nodeIndex == 1)
            {
                return "tlv_verticalempty";
            }
            else
            {
                return "tlv_verticalline";
            }
        }

        private void GetCenterHtml(StringBuilder sb, TreeListNode node)
        {
            if (node.ChildNodes.Count == 0)
            {
                if (node.IsLastNode)
                {
                    sb.Append("<td class='tlv_lastnochild'></td>");
                }
                else
                {
                    sb.Append("<td class='tlv_centernochild'></td>");
                }
            }
            else
            {
                sb.AppendFormat("<td class='tlv_centerhaschild'><img src=\"{0}\" id=\"img_{1}\" onClick=\"TreeListViewNodeClick(this);\" class='tlv_expandimg'></td>", node.RowInfo.CollapseIconUrl, node.RowInfo.ID);
            }
        }

        private void GetBottomHtml(StringBuilder sb, TreeListNode node)
        {
            if (node.ChildNodes.Count == 0)
            {
                if (node.IsLastNode)
                {
                    sb.Append("<td class='tlv_verticalempty'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>");
                }
                else
                {
                    sb.Append("<td class='tlv_verticalline2'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>");
                }
            }
            else
            {
                if (node.IsLastNode)
                {
                    sb.Append("<td class='tlv_verticalempty'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>");
                }
                else
                {
                    sb.Append("<td class='tlv_verticalline2'><img class='tlv_noneimg' src='images/TreeListView_None.gif' /></td>");
                }
            }
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.WriteEndTag("table");
            writer.Write("\n");
        }

        protected override void OnLoad(EventArgs e)
        {
            string strParm = "var TreeListView_Collapse=\"" + this.CollapseIconUrl + "\";\n";
            strParm += "var TreeListView_Expand=\"" + this.ExpandIconUrl + "\";\n";

            Page.ClientScript.RegisterClientScriptBlock(typeof(TreeListView), "TreeListViewScript", strParm + this._script, true);
            Page.ClientScript.RegisterClientScriptBlock(typeof(TreeListView), "TreeListViewStyle", this._style, false);

            base.OnLoad(e);
        }

        private string _border = "";
        public string Border
        {
            get { return _border; }
            set { _border = value; }
        }

        private string _width = "";
        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private string _height = "";
        public string Height
        {
            get { return _height; }
            set { _height = value; }
        }

        private string _cellpadding = "";
        public string CellPadding
        {
            get { return _cellpadding; }
            set { _cellpadding = value; }
        }

        private string _cellspacing = "";
        public string CellSpacing
        {
            get { return _cellspacing; }
            set { _cellspacing = value; }
        }

        private string _expandIconUrl = "images/TreeListView_Expand.gif";
        public string ExpandIconUrl
        {
            get { return _expandIconUrl; }
            set { _expandIconUrl = value; }
        }

        private string _collapseIconUrl = "images/TreeListView_Collapse.gif";
        public string CollapseIconUrl
        {
            get { return _collapseIconUrl; }
            set { _collapseIconUrl = value; }
        }

        private bool _showTreeLine = false;
        public bool ShowTreeLine
        {
            get { return _showTreeLine; }
            set { _showTreeLine = value; }
        }

        private Dictionary<string, string> _attributes = new Dictionary<string, string>();
        public Dictionary<string, string> Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }

        #region Static String

        private readonly string _style = @"
<style type=""text/css"">
.tlv_verticalline
{
	background-image: url('images/TreeViewList_Vertical.gif');
	background-repeat: repeat-y;
	background-position: center top;
	width:17px;
}
.tlv_verticalline2
{
	background-image: url('images/TreeViewList_Vertical2.gif');
	background-repeat: repeat-y;
	background-position: center top;
	width:17px;
}
.tlv_verticalempty
{
	width:17px;
}
.tlv_lastnochild
{
	background-image: url('images/TreeViewList_LastNoChildNode.gif');
	background-repeat:no-repeat;
	background-position: right top;
	width:17px;
	height:9px;
}
.tlv_centernochild
{
	background-image: url('images/TreeViewList_CenterNoChildNode.gif');
	background-repeat:no-repeat;
	background-position: right center;
	width:17px;
	height:9px;
}
.tlv_centerhaschild
{
	background-image: url('images/TreeViewList_Horizontal.gif');
	background-repeat: no-repeat;
	background-position: right center;
	width:17px;
	height:9px;
	text-align:center;
}
.tlv_rightline
{
	background-image: url('images/TreeViewList_Horizontal.gif');
	background-repeat: repeat-x;
	background-position: left center;
	width:5px;
}
.tlv_treelinetable
{
	height:100%;
	border:0px;
	border-collapse:collapse; 
	border-spacing:0px; 

}
.tlv_treelinetable th,td 
{ 
	padding: 0px; 
}
.tlv_expandimg
{
    height:9px;
    width:9px;
    cursor:pointer;
    border:0px;
}
.tlv_noneimg
{
    height:1px;
    width:17px;
    border:0px;
}
</style>";

        private readonly string _script = @"
function gid(id){return document.getElementById(id);}
function TreeListViewNodeClick(obj)
{
    var strDisplay=obj.src.indexOf(TreeListView_Collapse)>=0 ? ""none"":"""";
    var children=gid(obj.id.replace(""img_"","""")).getAttributeNode(""childids"").nodeValue;
    if (children==""""){obj.style.visibility=""hidden"";return;}
    var arr=children.split("","");
    for(var i=0,j=arr.length;i<j;i++)
    {
        gid(arr[i]).style.display=strDisplay;
        TreeListViewExpandNode(gid(arr[i]),strDisplay);
    }
    if (strDisplay==""none"")
        obj.src=TreeListView_Expand;
    else
        obj.src=TreeListView_Collapse;
    var span=gid(obj.id.replace(""img_"",""span_""));
    if (span) span.style.display=strDisplay;
}
function TreeListViewExpandNode(obj,mode)
{
    if(obj==null) return;
    var img=gid(""img_""+obj.id)
    if(img==null) return;
    var strDisplay=img.src.indexOf(TreeListView_Collapse)>=0 ? """":""none"";
    if (strDisplay==""none"" && mode=="""") return;
    var objchildren=gid(obj.id.replace(""img_"",""""));
    if(objchildren==null) return;
    var children=objchildren.getAttributeNode(""childids"").nodeValue;
    if (children=="""") return;
    var arr=children.split("","");
    for(var i=0,j=arr.length;i<j;i++)
    {
        gid(arr[i]).style.display=mode;
        TreeListViewExpandNode(gid(arr[i]),mode);
    }
}";

        #endregion
    }

    public class TreeListColumn
    {
        public TreeListColumn()
        {

        }


        public TreeListColumn(string text)
        {
            this._text = text;
        }

        public TreeListColumn(string text, string id)
        {
            this._text = text;
            this._id = id;
        }

        public TreeListColumn(string text, string id, string css)
        {
            this._text = text;
            this._id = id;
            this._cssClass = css;
        }

        internal string ExpandIconUrl = "";
        internal string CollapseIconUrl = "";

        private string _id = "";

        /// <summary>
        /// You must set this value
        /// </summary>
        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name = "";
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _text = "";
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private string _align = "";
        public string Align
        {
            get { return _align; }
            set { _align = value; }
        }

        private string _width = "";
        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private string _height = "";
        public string Height
        {
            get { return _height; }
            set { _height = value; }
        }

        private string _cssClass = "";
        public string CssClass
        {
            get { return _cssClass; }
            set { _cssClass = value; }
        }

        private string _onClientClick = "";
        public string OnClientClick
        {
            get { return _onClientClick; }
            set { _onClientClick = value; }
        }

        private Dictionary<string, string> _attributes = new Dictionary<string, string>();
        public Dictionary<string, string> Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }

    }

    public class TreeListNode : TreeNode
    {
        private TreeListColumn _rowInfo = new TreeListColumn();

        /// <summary>
        /// you must set RowInfo.ID(RowInfo.ID must only and can't contain ,")
        /// </summary>
        public TreeListColumn RowInfo
        {
            get { return _rowInfo; }
            set { _rowInfo = value; }
        }

        private bool _showCheckBox = false;
        public bool ShowCheckBox
        {
            get { return _showCheckBox; }
            set { _showCheckBox = value; }
        }

        private bool _isChecked = false;
        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                _isChecked = value;
            }
        }

        private bool _isExpanded = false;
        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                _isExpanded = value;
            }
        }

        private int _depth = 0;
        public int Depth
        {
            get
            {
                return _depth;
            }
            set
            {
                _depth = value;
            }
        }

        private bool m_IsLastNode = false;
        internal bool IsLastNode
        {
            get
            {
                return m_IsLastNode;
            }
            set
            {
                m_IsLastNode = value;
            }
        }

        private string _onCheckedFunName = "";
        public string OnCheckedFunName
        {
            get
            {
                return _onCheckedFunName;
            }
            set
            {
                _onCheckedFunName = value;
            }
        }

        private string _iconUrl = "";
        public string IconUrl
        {
            get
            {
                return _iconUrl;
            }
            set
            {
                _iconUrl = value;
            }
        }

        private List<TreeListIcon> _icons = new List<TreeListIcon>();
        public List<TreeListIcon> Icons
        {
            get { return _icons; }
            set { _icons = value; }
        }


        private List<TreeListColumn> _columns = new List<TreeListColumn>();
        public List<TreeListColumn> Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        public string GetChilds()
        {
            List<string> list = new List<string>();
            foreach (TreeListNode node in ChildNodes)
            {
                list.Add(node.RowInfo.ID);
            }
            return string.Join(",", list.ToArray());
        }
    }

    public class TreeListIcon
    {
        public TreeListIcon()
        {

        }

        public TreeListIcon(string url)
        {
            this.URL = url;
        }

        public TreeListIcon(string url, string width, string height)
        {
            this.URL = url;
            this.Width = width;
            this.Height = height;
        }

        private string _url;
        private string _width;
        private string _height;
        private string _border = "0";

        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }

        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public string Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public string Border
        {
            get { return _border; }
            set { _border = value; }
        }
    }

}
